package com.cn.jinl.controller;


import com.cn.jinl.api.IBoxManageService;
import com.cn.jinl.api.IOrderOutInfoService;
import com.cn.jinl.api.IProductStorageOutService;
import com.cn.jinl.domain.*;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.JsonResponse;
import com.cn.jinl.utils.QueryForm;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/productStorageOut")
public class ProductStorageOutController {

    @Autowired
    private IProductStorageOutService productStorageOutService;
    @Autowired
    private IBoxManageService boxManageService;
    @Autowired
    private IOrderOutInfoService orderOutInfoService;
    @RequestMapping("/getProductStorageOutList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductStorageOutVo>> getProductStorageOutList(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<List<ProductStorageOutVo>> response = new JsonResponse<>();
        List<ProductStorageOutVo> productStorages = productStorageOutService.getProductStorageOutList(queryForm);
        response.setResult(productStorages);
        response.setSuccess(true);
        return response;
    }

    @RequestMapping("/getProductStorageOutListMX/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductStorageOutVo>> getProductStorageOutListMX(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<List<ProductStorageOutVo>> response = new JsonResponse<>();
        List<ProductStorageOutVo> productStorages = productStorageOutService.getProductStorageOutListMX(queryForm);
        response.setResult(productStorages);
        response.setSuccess(true);
        return response;
    }

    @RequestMapping("/getProductStorageRelateCountMap/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Integer>> getProductStorageRelateCountMap(String storageOutId, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<Map<String,Integer>> response = new JsonResponse<>();
        Map<String,Integer> resultMap = productStorageOutService.getProductStorageRelateCountMap(storageOutId);
        response.setResult(resultMap);
        response.setSuccess(true);
        return response;
    }

    @RequestMapping("/getProductStorageByOrderId/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductStorageOrderVo>> getProductStorageByOrderId(String orderId, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<List<ProductStorageOrderVo>> response = new JsonResponse<>();
        List<ProductStorageOrderVo> results = productStorageOutService.getProductStorageByOrderId(orderId);
        response.setResult(results);
        response.setSuccess(true);
        return response;
    }


    @RequestMapping("/getProductStorageOutRelateListByOrderId/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductStorageOutRelate>> getProductStorageOutRelateListByOrderId(@RequestParam("orderId") String orderId, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<List<ProductStorageOutRelate>> response = new JsonResponse<>();
        List<ProductStorageOutRelate> productStorageRelates = productStorageOutService.getProductStorageOutRelateListByOrderId(orderId);
        response.setResult(productStorageRelates);
        response.setSuccess(true);
        return response;
    }

    //立即出库
    @RequestMapping("/api/updateBatchOutStorages")
    @ResponseBody
    public JsonResponse<String> addBatchProductStorageRelates(@RequestBody StorageOutVo storageOutVo) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = productStorageOutService.addBatchProductStorageRelates(storageOutVo);
        return CommonUtil.getResponseData(response,result);
    }


    //测试立即出库
    @RequestMapping("/api/updateTestBatchOutStorages")
    @ResponseBody
    public JsonResponse<String> updateTestBatchOutStorages() throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        StorageOutVo storageOutVo = new StorageOutVo();
        storageOutVo.setId("1");
        storageOutVo.setUserId("1");
        storageOutVo.setUserName("测试平库wq");
        List<ProductStorageOutRelate> storages = Lists.newArrayList();
        for(int i = 0 ; i< 1;i++){
            ProductStorageOutRelate relate1 = new ProductStorageOutRelate();
            relate1.setBoxCode("");
//            relate1.setRelateId("a8d8d74eorder-7666order-47e6order-8ac3order-69bb930c431c");
            relate1.setItemTypeId("单支连排");
            relate1.setGradeValue("长度130±2，内径6.0±0.2，壁厚0.28±0.05，切口角度50度±5，克重8.8克");
            storages.add(relate1);
        }
        storageOutVo.setStorages(storages);
        String result = productStorageOutService.addBatchProductStorageRelates(storageOutVo);
        return CommonUtil.getResponseData(response,result);
    }

    //app获取发货任务固定id为1的发货任务
    @RequestMapping("/api/getAPIOrderOutInfoListById")
    @ResponseBody
    public JsonResponse<List<OrderOutInfo>> getAPIOrderOutInfoList(QueryForm queryForm) throws Exception{
        JsonResponse<List<OrderOutInfo>> response = new JsonResponse<>();
        List<OrderOutInfo> orderOutInfos = orderOutInfoService.getAPIOrderOutInfoListById(queryForm);
        response.setResult(orderOutInfos);
        response.setSuccess(true);
        return response;
    }


    //立即出库
    @RequestMapping("/api/updateBatchOutStorages1")
    @ResponseBody
    public JsonResponse<String> updateBatchOutStorages1(@RequestBody StorageOutVo storageOutVo) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = productStorageOutService.addBatchProductStorageRelates1(storageOutVo);
        return CommonUtil.getResponseData(response,result);
    }
    //测试立即出库
    @RequestMapping("/api/updateTestBatchOutStorages1")
    @ResponseBody
    public JsonResponse<String> updateTestBatchOutStorages1() throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        StorageOutVo storageOutVo = new StorageOutVo();
        storageOutVo.setId("1");
        storageOutVo.setUserId("1");
        storageOutVo.setUserName("测试平库wq");
        List<ProductStorageOutRelate> storages = Lists.newArrayList();
        for(int i = 0 ; i< 1;i++){
            ProductStorageOutRelate relate1 = new ProductStorageOutRelate();
            relate1.setBoxCode("24239011");
//            relate1.setRelateId("a8d8d74eorder-7666order-47e6order-8ac3order-69bb930c431c");
            relate1.setItemTypeId("单支连排");
            relate1.setGradeValue("长度130±2，内径6.0±0.2，壁厚0.28±0.05，切口角度50度±5，克重8.8克");
            storages.add(relate1);
        }
        storageOutVo.setStorages(storages);
        String result = productStorageOutService.addBatchProductStorageRelates1(storageOutVo);
        return CommonUtil.getResponseData(response,result);
    }


    //作废
    @RequestMapping("/cancel/{frontToken}")
    @ResponseBody
    public JsonResponse<String> cancel(@RequestBody QueryForm queryForm,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = productStorageOutService.cancel(queryForm);
        return CommonUtil.getResponseData(response,result);
    }

    //清理出库扫码获取品类规格箱码
    @ResponseBody
    @RequestMapping("/api/cleanOutbound")
    public JsonResponse<BoxManageVo> cleanOutbound(String boxCode) throws Exception {
        JsonResponse<BoxManageVo> response = new JsonResponse<BoxManageVo>();
        BoxManageVo boxManageVo  = boxManageService.cleanOutbound(boxCode);
        response.setResult(boxManageVo);
        response.setSuccess(boxManageVo.getMsg() == null || boxManageVo.getMsg().isEmpty());
        return  response;
    }
}
